<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>根据属性计算平均值</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }

        .container {
            background-color: #fff;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 600px;
            box-sizing: border-box;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        label {
            font-size: 16px;
            color: #666;
            margin-bottom: 10px;
            display: block;
        }

        input[type="text"] {
            width: 100%;
            padding: 12px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .message {
            margin-top: 15px;
            color: #e74c3c;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>根据属性计算平均值</h1>

        <label for="inputData">请输入数据（JSON格式）：</label>
        <input id="inputData" type="text"
            placeholder='例如：[{"name": "张三", "score": 80}, {"name": "李四", "score": 90}, {"name": "王五", "score": 85}]' />

        <label for="handler">请输入属性名称（例如：score）：</label>
        <input id="handler" type="text" placeholder="例如：score" />

        <button onclick="calculateAverageBy()">计算平均值</button>

        <div id="result" class="result"></div>
        <div id="message" class="message"></div>
    </div>

    <script>
        const averageBy = (arr, handler) =>
            arr.map(typeof handler === "function" ? handler : val => val[handler])
                .reduce((acc, val) => acc + val, 0) / arr.length;

        function calculateAverageBy() {
            const inputData = document.getElementById('inputData').value;
            const handler = document.getElementById('handler').value;
            const resultDiv = document.getElementById('result');
            const messageDiv = document.getElementById('message');

            try {
                const arr = JSON.parse(inputData);
                if (Array.isArray(arr) && arr.length > 0 && handler) {
                    const avg = averageBy(arr, handler);
                    resultDiv.textContent = `平均值是：${avg}`;
                    messageDiv.textContent = ''; // 清除错误消息
                } else {
                    resultDiv.textContent = '';
                    messageDiv.textContent = '请输入有效的 JSON 数据和属性名。';
                }
            } catch (e) {
                resultDiv.textContent = '';
                messageDiv.textContent = '输入的格式无效，请确保是正确的 JSON 数据。';
            }
        }
    </script>

</body>

</html>